package cn.itcast.ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 登录界面
 */
public class LoginFrame extends JFrame {
    private static final long serialVersionUID = -7993721800179709451L;

    private ClientContext clientContext;

    public void setClientContext(ClientContext context) {
        this.clientContext = context;
    }

    public LoginFrame() {
        init();//初始化界面
    }

    private void init() {
        setSize(300, 180);
        setTitle("登录系统");
        setContentPane(createContentPane());
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                clientContext.exit(LoginFrame.this);
            }
        });
    }

    private JPanel createContentPane() {
        JPanel p = new JPanel(new BorderLayout());
        p.add(BorderLayout.NORTH, new JLabel("登录系统", JLabel.CENTER));
        p.add(BorderLayout.CENTER, createCenterPane());
        p.add(BorderLayout.SOUTH, createBtnPane());
        p.setBorder(new EmptyBorder(8, 8, 8, 8));
        return p;
    }

    private JPanel createCenterPane() {
        JPanel p = new JPanel(new BorderLayout());
        p.add(BorderLayout.NORTH, createIdPwdPane());
        p.setBorder(new EmptyBorder(6, 6, 6, 6));
        return p;
    }

    private JPanel createBtnPane() {
        JPanel p = new JPanel(new FlowLayout());
        JButton login = new JButton("登录");
        JButton cancel = new JButton("取消");
        p.add(login);
        p.add(cancel);

        getRootPane().setDefaultButton(login);

        login.addActionListener(e -> {
            clientContext.login();//将请求转发给控制器
        });
        cancel.addActionListener(e -> clientContext.exit(LoginFrame.this));

        return p;
    }

    private JPanel createIdPwdPane() {
        JPanel p = new JPanel(new GridLayout(2, 1, 0, 8));
        p.add(createIdPane());
        p.add(createPwdPane());
        return p;
    }

    private JPanel createIdPane() {
        JPanel p = new JPanel(new BorderLayout());
        JLabel l = new JLabel("编号:");
//  务必将idField 引用到界面控件对象
        idField = new JTextField();
        p.add(BorderLayout.WEST, l);
        p.add(BorderLayout.CENTER, idField);
        return p;
    }

    private JPanel createPwdPane() {
        JPanel p = new JPanel(new BorderLayout());
        JLabel l = new JLabel("密码:");
        //务必将pwdField 引用到界面控件对象
        pwdField = new JPasswordField();
        //处理一个Linux Swing 的BUG!
        pwdField.enableInputMethods(true);
        p.add(BorderLayout.WEST, l);
        p.add(BorderLayout.CENTER, pwdField);
        return p;
    }

    //务必将idField 引用到界面控件对象
    private JTextField idField;

    public String getUserId() {
        return idField.getText();
    }

    //务必将pwdField 引用到界面控件对象
    private JPasswordField pwdField;

    public String getPwd() {
        char[] pwd = pwdField.getPassword();
        return new String(pwd);
    }
}













